package com.shijichaoxing.service.impl;

import com.shijichaoxing.dao.CandidateDao;
import com.shijichaoxing.entity.Candidate;
import com.shijichaoxing.service.CandidateService;
import com.shijichaoxing.utils.PageParam;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zj
 */
@Service
public class CandidateServiceImpl implements CandidateService {
    @Resource
    private CandidateDao candidateDao;

    @Override
    public boolean addCandidate(Candidate candidate) {
        return candidateDao.addCandidate(candidate);
    }

    @Override
    public boolean updateCandidate(Candidate candidate) {
        return candidateDao.updateCandidate(candidate);
    }

    @Override
    public Candidate findCandidateById(Integer id) {
        return candidateDao.findCandidateById(id);
    }

    @Override
    public void findCandidateByPage(PageParam<Candidate> pageParam, String indexName, boolean isAsc) {
        int start = (pageParam.getCurrPage() - 1) * pageParam.getPageSize();
        indexName = switchInfo(indexName);
        pageParam.setData(candidateDao.findCandidateByPage(start, pageParam.getPageSize(), indexName, isAsc));
        pageParam.setRowCount(candidateDao.countAll());
    }

    @Override
    public boolean deleteById(Integer id) {
        return candidateDao.deleteById(id);
    }

    private String switchInfo(String indexName) {
        switch (indexName) {
            default:
                return "id";
            case "candidate_id":
            case "candidateId":
            case "id":
                return "id";
            case "realName":
            case "real_name":
                return "real_name";
            case "password":
                return "password";
            case "sex":
                return "sex";
            case "birthday":
                return "birthday";
            case "tel":
                return "tel";
            case "email":
                return "email";
            case "address":
                return "address";
            case "resumeUrl":
            case "resume_url":
                return "resume_url";
            case "graduationDate":
            case "graduation_date":
                return "graduation_date";
            case "employTime":
            case "employ_time":
                return "employ_time";
            case "interviewerId":
            case "interviewer_id":
                return "interviewer_id";
            case "comment":
                return "comment";
            case "createTime":
            case "create_time":
                return "create_time";
            case "updateTime":
            case "update_time":
                return "update_time";
        }
    }
}